#!/bin/bash
#qdu-rm-ai
#Author:sunhaihua

##prepration
sudo apt update &&sudo apt upgrade
sudo apt install git unzip cmake wget gcc g++
mkdir ~/base
cd ~/base

clear
echo $(pwd)

#Install google-test
googletest(){
    cd ~/base
    git clone https://gitee.com/c12h22o11/googletest.git 
    cd googletest && mkdir build && cd build && cmake ..

    if [ $? -eq 0 ];then 
	make -j8 && sudo make install
        echo -e "\033[32m google-test install successfully\033[0m"
    else
        echo -e "\033[31m google-test install failed\033[0m"   
        exit 
    fi

}
#Install BehaviorTree
BehaviorTree(){
    cd ~/base
	sudo apt-get install libzmq3-dev libboost-dev
	git clone https://gitee.com/c12h22o11/BehaviorTree.CPP.git
	cd BehaviorTree.CPP&& mkdir build && cd build && cmake CMAKE_INSTALL_PREFIX=/usr/local ..
	
    if [ $? -eq 0 ];then 
	    make -j8 && sudo make install
        sudo ldconfig
        echo -e "\033[32m BehaviorTree install successfully\033[0m"
    else
        echo -e "\033[31m BehaviorTree install failed\033[0m"   
        exit 
    fi


}
#Install spdlog
spdlog(){
    cd ~/base
	git clone https://gitee.com/c12h22o11/spdlog.git
	cd spdlog&& mkdir build && cd build && cmake CMAKE_INSTALL_PREFIX=/usr/local ..
	
    if [ $? -eq 0 ];then 
	    make -j8 && sudo make install
        echo -e "\033[32m spdlog install successfully\033[0m"
    else
        echo -e "\033[31m spdlog install failed\033[0m"   
        exit 
    fi


}

#Install MVS
MVS(){
    cd ~/base
    wget -O MVS.zip https://www.hikrobotics.com/cn2/source/support/client/MVS_Linux_STD_V2.1.0_201228.zip
    unzip MVS.zip
    find . -name  MVS*x86_64*.deb -exec sudo dpkg -i {} \;
    if [ $? -eq 0 ];then
        echo -e "\033[32m MVS  install successfully\033[0m"
        find -name "*MVS*" -exec rm -rf {} \;

    else 
        find -name "*MVS*" -exec rm -rf {} \;
        echo -e "\033[31m MVS install failed \033[0m"   
        exit

    fi
}
##Install opencv
opencv(){
    cd ~/base
    git clone https://gitee.com/c12h22o11/opencv.git && cd opencv && git switch -c 4.5.1
    sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev \
    libswscale-dev \
    python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev \
    libpng-dev libtiff-dev libdc1394-22-dev  ffmpeg libavcodec-dev \
    libavformat-dev libswscale-dev libavutil-de
    sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
    sudo apt install libjasper1 libjasper-dev
    mkdir build
	cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local  -D OPENCV_GENERATE_PKGCONFIG=ON -D WITH_FFMPEG=ON ..
    if [ $? -eq 0 ];then 
        make -j8 && sudo make install
    echo -e "\033[32mopencv install successfully\033[0m"
    else
        echo -e "\033[31m opencv install failed,please check\033[0m"   

    fi
}

##params select
echo -e "\033[34mThis is a shell for qdu-rm to install develop environment \033[0m"
echo -e "\033[34m Author sunhaihua \033[0m"
echo -e ""
echo -e "\033[32m1)configue and install google-test\033[0m"
echo -e "\033[32m2)configue and install Behavior Tree \033[0m"
echo -e "\033[32m3)configue and install spdlog \033[0m"   
echo -e "\033[32m4)configue and install MVS V2.1.0 \033[0m"
echo -e "\033[32m5)configue and install opencv\033[0m"
echo -e "\033[32m6)select all\033[0m"

echo 'Input your number select: '

read param

case $param in 
    1)
    echo -e "\033[32m1)configue and install google-test\033[0m"
    googletest
    ;;
    2)
    echo -e "\033[32mconfigue and install BehaviorTree \033[0m"
    BehaviorTree
    ;;
    3)
    echo -e "\033[32mconfigue and install spdlog \033[0m"
    spdlog
    ;;
    4)
    echo -e "\033[32mconfigue and install MVS \033[0m"
    MVS
    ;;
    5)
    echo -e "\033[32mconfigue and install opencv \033[0m"
    opencv
    ;;
    6)
    echo -e "\033[32m installing all files above\033[0m"
    googletest
    BehaviorTree
    spdlog
    MVS
    opencv
    ;;
esac
